import os
import sys
import tempfile

# 添加项目根目录到Python搜索路径
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from moviepy.video.io.VideoFileClip import VideoFileClip
from video_processors.info_processor import get_video_info_processor

# 创建一个简单的测试视频
print("创建测试视频...")
from moviepy.video.VideoClip import ColorClip

# 创建一个3秒的黑色视频，有简单的音频轨道
clip = ColorClip(size=(640, 480), color=(0, 0, 0), duration=3)
clip = clip.with_fps(30)  # 设置帧率

# 添加简单的音频（静默）
from moviepy.audio.AudioClip import AudioArrayClip
import numpy as np

# 创建1秒的静默音频
fps = 44100  # 音频采样率
n_samples = int(fps * 3)  # 3秒的采样点数量
silence = np.zeros((n_samples, 2), dtype=np.int16)  # 双声道静音

audio_clip = AudioArrayClip(silence, fps=fps)
clip = clip.set_audio(audio_clip)

# 保存测试视频
test_video_path = "test_video_with_audio.mp4"
clip.write_videofile(test_video_path, codec="libx264", audio_codec="aac", fps=24)
print(f"测试视频已创建: {test_video_path}")

# 验证修复
print("\n验证音频编解码器访问修复...")
try:
    # 直接调用修复后的处理函数
    video_info = get_video_info_processor(test_video_path)
    
    print("视频信息获取成功！")
    print("视频信息详情:")
    for key, value in video_info.items():
        print(f"- {key}: {value}")
        
    # 特殊检查音频编解码器字段
    if "audio_codec" in video_info:
        print(f"\n✓ 音频编解码器字段已正确处理: {video_info['audio_codec']}")
    else:
        print("\n✗ 音频编解码器字段缺失")
        
except Exception as e:
    print(f"\n✗ 测试失败: {str(e)}")

# 清理测试文件
try:
    if os.path.exists(test_video_path):
        os.remove(test_video_path)
        print(f"\n已清理测试视频文件: {test_video_path}")
except:
    pass

print("\n测试完成。")